package cc.rengu.igas.mcps.core.service.trans.scanpay;

import cc.rengu.igas.channel.alipay.facade.response.AliMircopayResponse;
import cc.rengu.igas.channel.upqc.facade.response.C2BConsumeResponse;
import cc.rengu.igas.channel.wechat.facade.response.WechatMicropayResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.igas.mcps.common.constant.McpsTreeNodeConstant;
import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.service.base.C2BPayService;
import cc.rengu.igas.mcps.facade.bean.OrderInfoBean;
import cc.rengu.igas.mcps.facade.request.PosScanQrCodeReceiptsRequest;
import cc.rengu.igas.mcps.facade.response.PosScanQrCodeReceiptsResponse;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.model.MchntTermInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.base.ChannelBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.*;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.TransCustomImpl;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.util.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 扫码收款申请服务
 * modify by fuyp on 20200311.
 */
public class PosScanQrCodeReceiptsService extends C2BPayService implements TransCustomImpl {

    @Override
    protected Object bizBeforeCust() throws Exception {
        PosScanQrCodeReceiptsRequest scanQrCodeReceiptsRequest = new PosScanQrCodeReceiptsRequest();
        ConvertUtil.convertOutput(scanQrCodeReceiptsRequest);
        return scanQrCodeReceiptsRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        PosScanQrCodeReceiptsRequest scanQrCodeReceiptsRequest = (PosScanQrCodeReceiptsRequest) request;
        String instId = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.INST_ID);
        String mchntNo = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.MCHNT_NO);

        /* Step1：条码支付交易查重 */
        checkUserOrderPayValidity(scanQrCodeReceiptsRequest);

        /* Step2：获取商户信息并校验 */
        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(instId, mchntNo);
        if (null == mchntInfo) {
            rglog.error("查询商户基本信息失败<{}>", mchntNo);
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }

        /* Step3：登记用户订单和商户订单 */
        OrderInfo orderInfo = new OrderInfo();
        OrderService orderService = new OrderServiceImpl();
        UserOrderInfo userOrderInfo = generateUserOrderInfo(scanQrCodeReceiptsRequest);
        MchntOrderInfo mchntOrderInfo = generateMchntOrderInfo(scanQrCodeReceiptsRequest, userOrderInfo);
        List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
        mchntOrderInfoList.add(mchntOrderInfo);
        orderInfo.setUserOrderInfo(userOrderInfo);
        orderInfo.setMchntOrderInfoList(mchntOrderInfoList);
        boolean iReturnCode = orderService.registerOrderInfo(userOrderInfo, mchntOrderInfoList, null);
        if (!iReturnCode) {
            rglog.error("登记支付订单信息失败!");
            throw new BizException(RespCodeEnum.INSERT_ORDER_INFO_ERROR.getRespCode(), RespCodeEnum.INSERT_ORDER_INFO_ERROR.getRespDesc());
        }

        /* Step4：判断商户限额是否超限 */
        LimitAcctTypeEnum limitAcctTypeEnum = LimitAcctTypeEnum.ALL;
        if ("02".equals(txnInfo.getSrcTxnAuthCfg().getBizType())) {
            String qrCodeType = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.QR_CODE_TYPE);
            if (StringUtil.isEmptyOrNull(qrCodeType) || (!"D".equals(qrCodeType) && !"C".equals(qrCodeType))) {
                rglog.error("未知的银联条码类型，交易不支持!");
                updateFailOrderInfo(userOrderInfo, mchntOrderInfo);
                throw new BizException(RespCodeEnum.POS_FORMAT_ERROR.getRespCode(), RespCodeEnum.POS_FORMAT_ERROR.getRespDesc());
            }
            limitAcctTypeEnum = "D".equals(qrCodeType) ? LimitAcctTypeEnum.OTHER_DEBIT : LimitAcctTypeEnum.OTHER_CREDIT;
        }
        int limitPayFlag = mchntService.checkMchntTransLimit(instId, scanQrCodeReceiptsRequest.getTransAmt(), limitAcctTypeEnum, scanQrCodeReceiptsRequest.getMchntNo(), null, txnInfo.getSrcTxnAuthCfg().getTxnNum());
        if (0 < limitPayFlag) {
            rglog.error("商户限额超限{}", mchntNo);
            updateFailOrderInfo(userOrderInfo, mchntOrderInfo);
            throw new BizException(RespCodeEnum.POS_OVER_AMT_LIMIT.getRespCode(), RespCodeEnum.POS_OVER_AMT_LIMIT.getRespDesc());
        }

        /* Step5：调用通道接口 */
        if (null == txnInfo.getChannelCallCfgList() || txnInfo.getChannelCallCfgList().isEmpty()) {
            rglog.error("<{}-{}>交易未配置交易通道服务调用配置信息！", txnInfo.getSrcTxnAuthCfg().getTxnNum(), txnInfo.getSrcTxnAuthCfg().getTxnName());
            throw new BizException(RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespCode(), RespCodeEnum.CHANNEL_CONFIG_NOT_EXIST.getRespDesc());
        }
        List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item ->
                item.getCallIndex().equals(ChannelBaseService.FIRST_CHANNEL_CALL_INDEX)).collect(Collectors.toList());
        ChannelCallCfg channelCallCfg = channelCallSceneDiscern(scanQrCodeReceiptsRequest, channelCallCfgList);
        Class channelClasz = Class.forName(channelCallCfg.getCallChannelImpl());
        ChannelBaseService channelBaseService = (ChannelBaseService) channelClasz.newInstance();
        channelBaseService.callService(txnInfo, orderInfo, channelCallCfg.getBizScene(), request);

        /* Step6：更新商户累计限额 */
        if (BizStatusEnum.FINISH.getStatus().equals(orderInfo.getUserOrderInfo().getBizStatus()) &&
                OrderStatusEnum.FINISH.getStatus().equals(orderInfo.getUserOrderInfo().getOrderStatus())) {
            mchntService.updateMchntTransLimit(orderInfo.getMchntOrderInfoList());
        }

        /* Step7：组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        PosScanQrCodeReceiptsResponse posScanQrCodeReceiptsResponse = (PosScanQrCodeReceiptsResponse) GlobalTransUtil.getCacheValue(TransCacheConstant.BIZ_RESPONSE);
        posScanQrCodeReceiptsResponse = null == posScanQrCodeReceiptsResponse ? new PosScanQrCodeReceiptsResponse() : posScanQrCodeReceiptsResponse;
        posScanQrCodeReceiptsResponse.setHeader(scanQrCodeReceiptsRequest.getHeader());
        posScanQrCodeReceiptsResponse.setAcqInstCode(mchntInfo.getSignInstId());
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(posScanQrCodeReceiptsResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        PosScanQrCodeReceiptsResponse scanQrCodeReceiptsResponse = (PosScanQrCodeReceiptsResponse) bizResponse.getResult();
        scanQrCodeReceiptsResponse.setRespCode(bizResponse.getRespCode());
        scanQrCodeReceiptsResponse.setRespDesc(bizResponse.getRespDesc());
        ConvertUtil.convertInput(scanQrCodeReceiptsResponse);
    }

    /**
     * 生成用户订单信息
     *
     * @param posScanQrCodeReceiptsRequest 扫码请求信息
     * @return 用户订单信息
     * @throws Exception 异常
     */
    private UserOrderInfo generateUserOrderInfo(PosScanQrCodeReceiptsRequest posScanQrCodeReceiptsRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String userId = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.USER_ID);
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        /*初始化用户订单信息*/
        userOrderInfo.setInstId(posScanQrCodeReceiptsRequest.getHeader().getInstId());
        userOrderInfo.setTxnNum(posScanQrCodeReceiptsRequest.getHeader().getTxnNum());
        userOrderInfo.setBizType(posScanQrCodeReceiptsRequest.getHeader().getBizType());
        userOrderInfo.setMsgSrcId(posScanQrCodeReceiptsRequest.getHeader().getSrcSysId());
        userOrderInfo.setTransChannelId(this.txnInfo.getSrcTxnAuthCfg().getTransChannelId());
        userOrderInfo.setMchntOrderId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        userOrderInfo.setUserOrderId(sysSeqNum);
        userOrderInfo.setSysSeqNum(sysSeqNum);
        userOrderInfo.setOrderAmt(posScanQrCodeReceiptsRequest.getTransAmt());
        userOrderInfo.setOrderCcy(AppParamConstant.DEFAULT_ORDER_CCY);
        userOrderInfo.setTxnDate(DateUtil.getCurrentDate());
        userOrderInfo.setTxnTime(DateUtil.getCurrentTime());
        userOrderInfo.setOrderDesc(posScanQrCodeReceiptsRequest.getGoodDesc());
        /*设置实际支付金额为订单金额*/
        userOrderInfo.setNeedPayAmt(userOrderInfo.getOrderAmt());
        /*设置实际支付金额*/
        userOrderInfo.setRealPayAmt(userOrderInfo.getOrderAmt());
        /*设置用户标识*/
        userOrderInfo.setUserId(userId);
        /*设置订单失效时间*/
        /*设置前端流水号*/
        userOrderInfo.setFrontSeqNum(posScanQrCodeReceiptsRequest.getHeader().getTraceNo());
        userOrderInfo.setOrderCreateTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"));
        userOrderInfo.setOrderExpiryTime(DateUtil.getDateByTimeStamp(System.currentTimeMillis() + 5 * 60 * 1000, "yyyyMMddHHmmss"));
        userOrderInfo.setOrderStatus(OrderStatusEnum.INIT.getStatus());
        userOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        userOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        userOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        return userOrderInfo;
    }

    /**
     * 生成商户订单信息
     *
     * @param scanQrCodeReceiptsRequest 扫码请求信息
     * @param userOrderInfo             用户订单信息
     * @return 商户户订单信息
     * @throws Exception 异常
     */
    private MchntOrderInfo generateMchntOrderInfo(PosScanQrCodeReceiptsRequest scanQrCodeReceiptsRequest, UserOrderInfo userOrderInfo) throws Exception {
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        BeanUtil.beanCopy(userOrderInfo, mchntOrderInfo);
        /*设置商户订单中其他字段值,需要查询商户信息表，获取商户相关信息*/
        mchntOrderInfo.setMchntNo(scanQrCodeReceiptsRequest.getMchntNo());
        /*若存在 设置连锁商户门店*/
        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(mchntOrderInfo.getInstId(), mchntOrderInfo.getMchntNo());
        if (null != mchntInfo.getChainMchntNo()) {
            mchntOrderInfo.setChainMchntNo(mchntInfo.getChainMchntNo());
        }
        mchntOrderInfo.setRealSettleAmt(scanQrCodeReceiptsRequest.getTransAmt());
        mchntOrderInfo.setMchntName(mchntInfo.getMchntName());
        mchntOrderInfo.setTermNo(scanQrCodeReceiptsRequest.getTermNo());
        MchntTermInfo mchntTermInfo = mchntService.getMchntTermInfo(mchntOrderInfo.getInstId(), mchntOrderInfo.getMchntNo(), mchntOrderInfo.getTermNo());
        mchntOrderInfo.setTermType(mchntTermInfo.getTermType());
        mchntOrderInfo.setMchntSettleAmt("0");
        mchntOrderInfo.setUserId(scanQrCodeReceiptsRequest.getUserId());
        mchntOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        mchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        mchntOrderInfo.setIndustryType(mchntInfo.getIndustryType());
        /*收单机构号*/
        mchntOrderInfo.setAcqInstCode(mchntInfo.getSignInstId());
        /*业务产品代码*/
        mchntOrderInfo.setBizProdCode(this.txnInfo.getSrcTxnAuthCfg().getBizProdCode());
        mchntOrderInfo.setRealSettleFlag("N");
        /*保存连锁商户于商户订单表,连锁门店并且非独立清算*/
        if ("11".equals(mchntInfo.getMchntType()) && "N".equals(mchntInfo.getIndependentDataFlag())) {
            mchntOrderInfo.setChainMchntNo(mchntInfo.getChainMchntNo());
        }
        return mchntOrderInfo;
    }

    /**
     * 更新用户订单、商户订单
     *
     * @param userOrderInfo  用户订单信息
     * @param mchntOrderInfo 商户订单信息
     * @throws Exception 异常
     */
    private void updateFailOrderInfo(UserOrderInfo userOrderInfo, MchntOrderInfo mchntOrderInfo) throws Exception {
        /* 更新用户订单信息 */
        userOrderInfo.setOrderStatus(OrderStatusEnum.CLOSE.getStatus());
        userOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
        userOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        /* 更新商户订单信息 */
        mchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
        mchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
        List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
        mchntOrderInfoList.add(mchntOrderInfo);
        OrderService orderService = new OrderServiceImpl();
        boolean iReturnCode = orderService.updateOrderInfo(userOrderInfo, mchntOrderInfoList, null);
        if (!iReturnCode) {
            rglog.error("更新订单信息失败!");
            throw new BizException(RespCodeEnum.INSERT_ORDER_INFO_ERROR.getRespCode(), RespCodeEnum.INSERT_ORDER_INFO_ERROR.getRespDesc());
        }
    }

    /**
     * 条码收款交易校验查重
     *
     * @param scanQrCodeReceiptsRequest 请求对象
     * @throws Exception 异常
     */
    private void checkUserOrderPayValidity(PosScanQrCodeReceiptsRequest scanQrCodeReceiptsRequest) throws Exception {
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        String frontSeqNum = scanQrCodeReceiptsRequest.getHeader().getTraceNo();
        UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByFrontSeqNum(scanQrCodeReceiptsRequest.getHeader().getInstId(),
                frontSeqNum, scanQrCodeReceiptsRequest.getHeader().getSrcSysId(), this.txnInfo.getSrcTxnAuthCfg().getTransChannelId());
        if (null != userOrderInfo) {
            if (OrderStatusEnum.CLOSE.getStatus().equals(userOrderInfo.getOrderStatus())) {
                rglog.info("用户订单已关闭.orderStatus:<{}>", userOrderInfo.getOrderStatus());
                throw new BizException(RespCodeEnum.ORDER_CLOSED.getRespCode(), RespCodeEnum.ORDER_CLOSED.getRespDesc());
            }
            if (OrderStatusEnum.FINISH.getStatus().equals(userOrderInfo.getOrderStatus())) {
                rglog.info("用户订单已完成.orderStatus:<{}>", userOrderInfo.getOrderStatus());
                throw new BizException(RespCodeEnum.ORDER_FINISH.getRespCode(), RespCodeEnum.ORDER_FINISH.getRespDesc());
            }
            if (BizStatusEnum.PROCESSING.getStatus().equals(userOrderInfo.getBizStatus())) {
                rglog.info("用户订单处理处理中，请发起查询支付状态。bizStatus:<{}>", userOrderInfo.getBizStatus());
                throw new BizException(RespCodeEnum.TRANS_PROCESSING.getRespCode(), RespCodeEnum.TRANS_PROCESSING.getRespDesc());
            }
            if (!scanQrCodeReceiptsRequest.getTransAmt().equals(userOrderInfo.getOrderAmt())) {
                rglog.info("订单金额不一致，transAmt:<{}>,orderAmt:<{}>", scanQrCodeReceiptsRequest.getTransAmt(), userOrderInfo.getOrderAmt());
                throw new BizException(RespCodeEnum.TRANS_AMT_DISACCORD.getRespCode(), RespCodeEnum.TRANS_AMT_DISACCORD.getRespDesc());
            }
            if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"), userOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") > 0) {
                rglog.info("订单已经过了有效期，当前日期curDateTime:<{}>,订单有效期orderExpiryTime:<{}>", DateUtil.getCurrentDateTime("yyyyMMddHHmmss"), userOrderInfo.getOrderExpiryTime());
                throw new BizException(RespCodeEnum.ORDER_EXPR_ERROR.getRespCode(), RespCodeEnum.ORDER_EXPR_ERROR.getRespDesc());
            }
        }
    }

    @Override
    public ChannelCallCfg channelCallSceneDiscern(Object request, List<ChannelCallCfg> channelCallCfgList) throws Exception {
        /* 业务细分：bizType：00-微信条码支付，01-支付宝条码支付，02-银联二维码条码支付 */
        Optional<ChannelCallCfg> channelCallCfgOpt = channelCallCfgList.stream().filter(item ->
                item.getBizScene().equals(ShareParamConstant.CALL_CFG_DEFAULT_BIZ_SCENE)).findFirst();
        return channelCallCfgOpt.orElse(null);
    }

    @Override
    public CheckResultEnum channelCallPreCheck(Object request, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        /* 支付结果查询，先判断订单状态是否为处理中 */
        if ("0001".equals(channelCallCfg.getCallIndex())) {
            Optional<PayOrderInfo> payOrderInfoOpt = orderInfo.getPayOrderInfoList().stream().filter(item ->
                    item.getMsgDstId().equals(ChannelEnum.UPWX.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.NUWX.getChannelType()) ||
                            item.getMsgDstId().equals(ChannelEnum.WXZF.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.UPAL.getChannelType()) ||
                            item.getMsgDstId().equals(ChannelEnum.NUAL.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.ALIP.getChannelType()) ||
                            item.getMsgDstId().equals(ChannelEnum.UPQC.getChannelDesc())).findFirst();
            if (payOrderInfoOpt.isPresent()) {
                if (payOrderInfoOpt.get().getBizStatus().equals(BizStatusEnum.PROCESSING.getStatus())) {
                    return CheckResultEnum.PASSED;
                } else {
                    return CheckResultEnum.NEXT;
                }
            } else {
                return CheckResultEnum.NEXT;
            }
        } else {
            return CheckResultEnum.PASSED;
        }
    }

    @Override
    public TransStatusEnum channelCallResultProc(Object request, ChannelCallCfg channelCallCfg, TransStatusEnum transStatusEnum, Object channelResponse, OrderInfo needUpdateOrderInfo) throws Exception {
        if (TransStatusEnum.TRANS_SUCC.equals(transStatusEnum)) {
            PosScanQrCodeReceiptsResponse posScanQrCodeReceiptsResponse = (PosScanQrCodeReceiptsResponse) GlobalTransUtil.getCacheValue(TransCacheConstant.BIZ_RESPONSE);
            if (null == posScanQrCodeReceiptsResponse) {
                posScanQrCodeReceiptsResponse = new PosScanQrCodeReceiptsResponse();
            }
            OrderInfoBean orderInfo = new OrderInfoBean();
            posScanQrCodeReceiptsResponse.setOrderInfo(orderInfo);
            Optional<PayOrderInfo> payOrderInfoOpt = needUpdateOrderInfo.getPayOrderInfoList().stream().filter(item ->
                    item.getMsgDstId().equals(channelCallCfg.getCallChannelId()) && item.getDstCallIndex().equals(channelCallCfg.getCallIndex())).findFirst();
            if (payOrderInfoOpt.isPresent()) {
                BeanUtil.beanCopy(payOrderInfoOpt.get(), orderInfo);
                BeanUtil.nullValueCopy(payOrderInfoOpt.get(), orderInfo);
            }
            if (null != needUpdateOrderInfo.getUserOrderInfo()) {
                orderInfo.setOrderStatus(needUpdateOrderInfo.getUserOrderInfo().getOrderStatus());
            }
            if (ChannelEnum.UPWX.getChannelType().equals(channelCallCfg.getCallChannelId()) ||
                    ChannelEnum.NUWX.getChannelType().equals(channelCallCfg.getCallChannelId()) ||
                    ChannelEnum.WXZF.getChannelType().equals(channelCallCfg.getCallChannelId())) {
                Result<WechatMicropayResponse> callResult = (Result<WechatMicropayResponse>) channelResponse;
                if (null != callResult && null != callResult.getResult()) {
                    WechatMicropayResponse wechatMicropayResponse = callResult.getResult();
                    if (!StringUtil.isEmptyOrNull(wechatMicropayResponse.getTimeEnd())) {
                        posScanQrCodeReceiptsResponse.setSettleDate(wechatMicropayResponse.getTimeEnd().substring(0, 8));
                        payOrderInfoOpt.ifPresent(payOrderInfo -> payOrderInfo.setSettleDate(wechatMicropayResponse.getTimeEnd().substring(0, 8)));
                    }
                }
            } else if (ChannelEnum.UPAL.getChannelType().equals(channelCallCfg.getCallChannelId()) ||
                    ChannelEnum.NUAL.getChannelType().equals(channelCallCfg.getCallChannelId()) ||
                    ChannelEnum.ALIP.getChannelType().equals(channelCallCfg.getCallChannelId())) {
                cc.rengu.igas.channel.alipay.facade.result.Result<AliMircopayResponse> callResult = (cc.rengu.igas.channel.alipay.facade.result.Result<AliMircopayResponse>) channelResponse;
                if (null != callResult && null != callResult.getResult()) {
                    AliMircopayResponse aliMircopayResponse = callResult.getResult();
                    if (!StringUtil.isEmptyOrNull(aliMircopayResponse.getPayTime())) {
                        posScanQrCodeReceiptsResponse.setSettleDate(aliMircopayResponse.getPayTime().replaceAll("-", "").substring(0, 8));
                        payOrderInfoOpt.ifPresent(payOrderInfo -> payOrderInfo.setSettleDate(aliMircopayResponse.getPayTime().substring(0, 8)));
                    }
                }
            } else if (ChannelEnum.UPQC.getChannelType().equals(channelCallCfg.getCallChannelId())) {
                cc.rengu.igas.channel.upqc.facade.result.Result<C2BConsumeResponse> callResult = (cc.rengu.igas.channel.upqc.facade.result.Result<C2BConsumeResponse>) channelResponse;
                C2BConsumeResponse c2BConsumeResponse = callResult.getResult();
                posScanQrCodeReceiptsResponse.setSettleDate(c2BConsumeResponse.getSettleDate());
                payOrderInfoOpt.ifPresent(payOrderInfo -> payOrderInfo.setChannelReconKey(c2BConsumeResponse.getSettleKey()));
                payOrderInfoOpt.ifPresent(payOrderInfo -> payOrderInfo.setSettleDate(c2BConsumeResponse.getSettleDate()));
            }
            GlobalTransUtil.setCacheValue(TransCacheConstant.BIZ_RESPONSE, posScanQrCodeReceiptsResponse);
        }
        return transStatusEnum;
    }
}
